﻿namespace WMS6.Web.Analytics
{
    using System;
    using System.Data;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.AnalyticsReportService2Ref;
    using WMS6.Web.UserControls;

    public class BillingProfitRptList : BasePage
    {
        protected HtmlInputButton btnAction;
        protected Button btnExport;
        protected Button btnList;
        protected Button btnRefresh;
        protected Button btnReSearch;
        protected Button btnSearch;
        protected HtmlForm F15671;
        protected GridView gvBillingProfit;
        protected HtmlInputHidden hdnActionID;
        protected HtmlInputHidden hdnFrom;
        protected Label lblAllTotalAmount;
        protected Label lblAllTotalAmountText;
        protected Label lblBillingProfitList;
        protected Label lblMsg;
        protected HtmlLink mycss;
        protected Pager PagerFooter;
        protected ScriptManager ScriptManager1;
        private decimal totalAmount;
        protected UpdatePanel up1;

        private void BindData2BillingProfitList(InvoiceRptQueryEntity invoiceRptQueryEntity)
        {
            this.PagerFooter.CurrentPage = int.Parse(invoiceRptQueryEntity.CurrentPage);
            DataSet ds = AnalyticsReportService2ClientProxy.GetInstance().GetBillingProfitRptDataSet(invoiceRptQueryEntity);
            this.gvBillingProfit.Visible = true;
            base.BindData2GridView(this.gvBillingProfit, ds);
            if ((ds != null) && (ds.Tables.Count > 1))
            {
                this.PagerFooter.PageInfo = ds.Tables[1].Rows[0]["CurrentPage"].ToString() + " / " + ds.Tables[1].Rows[0]["TotalPagesCount"].ToString() + "&nbsp;&nbsp;&nbsp;共: " + ds.Tables[1].Rows[0]["TotalRecordsCount"].ToString();
                this.PagerFooter.TotalPages = int.Parse(ds.Tables[1].Rows[0]["TotalPagesCount"].ToString());
            }
        }

        protected void btnExport_Click(object sender, EventArgs e)
        {
            InvoiceRptQueryEntity invoiceRptQueryEntity = this.GetInvoiceQueryEntity();
            invoiceRptQueryEntity.IsGetAll = true;
            this.hdnActionID.Value = "EXPORT";
            string currentPageIndex = invoiceRptQueryEntity.CurrentPage;
            invoiceRptQueryEntity.CurrentPage = "0";
            try
            {
                using (DataSet ds = AnalyticsReportService2ClientProxy.GetInstance().GetBillingProfitRptDataSet(invoiceRptQueryEntity))
                {
                    base.Export2Excel("BillingProfitRpt.xls", "15671", this.gvBillingProfit, ds);
                }
            }
            finally
            {
                invoiceRptQueryEntity.IsGetAll = false;
                invoiceRptQueryEntity.CurrentPage = currentPageIndex;
                this.SetInvoiceQueryEntity(invoiceRptQueryEntity);
            }
        }

        protected void btnFirst_Click(object sender, EventArgs e)
        {
            InvoiceRptQueryEntity invoiceRptQueryEntity = this.GetInvoiceQueryEntity();
            invoiceRptQueryEntity.CurrentPage = "0";
            this.BindData2BillingProfitList(invoiceRptQueryEntity);
        }

        protected void btnLast_Click(object sender, EventArgs e)
        {
            InvoiceRptQueryEntity invoiceRptQueryEntity = this.GetInvoiceQueryEntity();
            invoiceRptQueryEntity.CurrentPage = (this.PagerFooter.TotalPages - 1).ToString();
            this.BindData2BillingProfitList(invoiceRptQueryEntity);
        }

        protected void btnList_Click(object sender, EventArgs e)
        {
            InvoiceRptQueryEntity invoiceRptQueryEntity = this.GetInvoiceQueryEntity();
            invoiceRptQueryEntity.BILLING_OBJECT = "";
            invoiceRptQueryEntity.BILLING_OBJECT_ID = "";
            invoiceRptQueryEntity.DUE_FROM_TO = "";
            invoiceRptQueryEntity.StartDateTime = "";
            invoiceRptQueryEntity.EndDateTime = "";
            this.BindData2BillingProfitList(invoiceRptQueryEntity);
        }

        protected void btnNext_Click(object sender, EventArgs e)
        {
            InvoiceRptQueryEntity invoiceRptQueryEntity = this.GetInvoiceQueryEntity();
            if (int.Parse(invoiceRptQueryEntity.CurrentPage) < (this.PagerFooter.TotalPages - 1))
            {
                invoiceRptQueryEntity.CurrentPage = (int.Parse(invoiceRptQueryEntity.CurrentPage) + 1).ToString();
                this.BindData2BillingProfitList(invoiceRptQueryEntity);
            }
        }

        protected void btnPrevious_Click(object sender, EventArgs e)
        {
            InvoiceRptQueryEntity invoiceRptQueryEntity = this.GetInvoiceQueryEntity();
            if (int.Parse(invoiceRptQueryEntity.CurrentPage) > 0)
            {
                invoiceRptQueryEntity.CurrentPage = (int.Parse(invoiceRptQueryEntity.CurrentPage) - 1).ToString();
                this.BindData2BillingProfitList(invoiceRptQueryEntity);
            }
        }

        protected void btnRefresh_Click(object sender, EventArgs e)
        {
            InvoiceRptQueryEntity invoiceRptQueryEntity = this.GetInvoiceQueryEntity();
            this.BindData2BillingProfitList(invoiceRptQueryEntity);
        }

        private InvoiceRptQueryEntity GetInvoiceQueryEntity()
        {
            return (base.PageQueryEntitySession as InvoiceRptQueryEntity);
        }

        protected void GoTo_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(((TextBox) this.PagerFooter.FindControl("txtGo2Page")).Text.Trim()))
            {
                InvoiceRptQueryEntity invoiceRptQueryEntity = this.GetInvoiceQueryEntity();
                invoiceRptQueryEntity.CurrentPage = this.PagerFooter.GoToPage.ToString();
                this.BindData2BillingProfitList(invoiceRptQueryEntity);
            }
        }

        protected void gvBillingProfit_Init(object sender, EventArgs e)
        {
            base.InitDetailGridView("F15671", this.gvBillingProfit);
        }

        protected void gvBillingProfit_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[0].Text = (((this.PagerFooter.CurrentPage * this.PagerFooter.PageSize) + e.Row.RowIndex) + 1).ToString();
                e.Row.Attributes.Add("onmouseout", "GridViewRowMouseout(this);");
                e.Row.Attributes.Add("onmouseover", "GridViewRowMouseover(this);");
                ((CheckBox) e.Row.FindControl("chbSelect")).Attributes.Add("onclick", "GridViewCheckBoxClick('" + e.Row.ClientID + "')");
                e.Row.Attributes.Add("ondblclick", "JavaScript:document.getElementById('" + ((CheckBox) e.Row.FindControl("chbSelect")).ClientID + "').click();");
                if (this.hdnActionID.Value != "EXPORT")
                {
                }
                if ((((Label) e.Row.FindControl("lblBillingAmount")) != null) && !string.IsNullOrEmpty(((Label) e.Row.FindControl("lblBillingAmount")).Text))
                {
                    this.totalAmount += decimal.Parse(((Label) e.Row.FindControl("lblBillingAmount")).Text);
                }
            }
            if (e.Row.RowType == DataControlRowType.Footer)
            {
                this.lblAllTotalAmountText.Text = double.Parse(this.totalAmount.ToString()).ToString();
            }
        }

        protected void gvBillingProfit_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
                InvoiceRptQueryEntity invoiceRptQueryEntity = this.GetInvoiceQueryEntity();
                invoiceRptQueryEntity.SortField = e.SortExpression;
                if (invoiceRptQueryEntity.SortDirection == "ASC")
                {
                    invoiceRptQueryEntity.SortDirection = "DESC";
                }
                else if (invoiceRptQueryEntity.SortDirection == "DESC")
                {
                    invoiceRptQueryEntity.SortDirection = "ASC";
                }
                this.BindData2BillingProfitList(invoiceRptQueryEntity);
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        private void LoadData()
        {
            InvoiceRptQueryEntity invoiceRptQueryEntity = new InvoiceRptQueryEntity();
            if (string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                if (this.PreviousPage != null)
                {
                    invoiceRptQueryEntity.BILLING_OBJECT = this.PreviousPage.BillingObject;
                    invoiceRptQueryEntity.BILLING_OBJECT_ID = this.PreviousPage.BillingObjectID;
                    invoiceRptQueryEntity.DUE_FROM_TO = this.PreviousPage.DueFromTo;
                    invoiceRptQueryEntity.StartDateTime = this.PreviousPage.StartDate;
                    invoiceRptQueryEntity.EndDateTime = this.PreviousPage.EndDate;
                }
                else
                {
                    invoiceRptQueryEntity.BILLING_OBJECT = "";
                    invoiceRptQueryEntity.BILLING_OBJECT_ID = "";
                    invoiceRptQueryEntity.DUE_FROM_TO = "";
                    invoiceRptQueryEntity.StartDateTime = "";
                    invoiceRptQueryEntity.EndDateTime = "";
                }
            }
            else
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
            invoiceRptQueryEntity.IsGetAll = false;
            invoiceRptQueryEntity.CurrentPage = "0";
            invoiceRptQueryEntity.PageSize = this.gvBillingProfit.PageSize.ToString();
            invoiceRptQueryEntity.SortField = " BILLING_OBJECT_ID";
            invoiceRptQueryEntity.SortDirection = "ASC";
            this.PagerFooter.PageSize = this.gvBillingProfit.PageSize;
            this.SetInvoiceQueryEntity(invoiceRptQueryEntity);
            this.BindData2BillingProfitList(invoiceRptQueryEntity);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                base.InitAnalyticsReportGridview(this.gvBillingProfit);
                this.LoadData();
            }
        }

        private void SetInvoiceQueryEntity(InvoiceRptQueryEntity invoiceRptQueryEntity)
        {
            base.PageQueryEntitySession = invoiceRptQueryEntity;
        }

        public BillingProfitRptQuery PreviousPage
        {
            get
            {
                return (BillingProfitRptQuery) base.PreviousPage;
            }
        }
    }
}

